﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Security;
namespace RMS.Common
{
    public static class CookiesHelper
    {

        #region 添加Cookies
        /// <summary>
        /// 添加Cookie(Key和Value都会进行DES加密)
        /// </summary>
        /// <param name="cookie"></param>
        public static void AddCookie(HttpCookie cookie)
        {
            var response = HttpContext.Current.Response;
            if (response == null) return;
            var domain = FormsAuthentication.CookieDomain;
            if (!string.IsNullOrEmpty(domain))
                cookie.Domain = domain;

            //指定客户端脚本是否可以访问,为True表示不可访问。[默认为false]
            cookie.HttpOnly = true;

            response.AppendCookie(cookie);
        }

        /// <summary>
        /// 添加Cookie(Key和Value都会进行DES加密)
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void AddCookie(string key, string value)
        {
            var cookie = new HttpCookie(key, value) { Expires = DateTime.Now.AddDays(10) };
            AddCookie(cookie);
        }

        /// <summary>
        /// 添加Cookie(Key和Value都会进行DES加密)
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="expires"></param>
        public static void AddCookie(string key, string value, DateTime expires)
        {
            var cookie = new HttpCookie(key, value) { Expires = expires };
            AddCookie(cookie);
        }

        /// <summary>
        /// 添加为Cookie.Values集合,Expires为最短时间


        /// (Key和Value都会进行DES加密)
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void AddCookie(string cookieName, string key, string value)
        {
            var cookie = new HttpCookie(cookieName) { Expires = DateTime.Now.AddDays(10) };
            cookie[key] = value;
            AddCookie(cookie);
        }

        /// <summary>
        /// 添加为Cookie集合
        /// (Key和Value都会进行DES加密)
        /// </summary>
        /// <param name="cookieName">Cookie名称</param>
        /// <param name="expires">过期时间</param>
        public static void AddCookie(string cookieName, DateTime expires)
        {
            var cookie = new HttpCookie(cookieName) { Expires = expires };
            AddCookie(cookie);
        }

        /// <summary>
        /// 添加为Cookie.Values集合
        /// (Key和Value都会进行DES加密)
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="expires"></param>
        public static void AddCookie(string cookieName, string key, string value, DateTime expires)
        {
            var cookie = new HttpCookie(cookieName) { Expires = expires };
            cookie[key] = value;
            AddCookie(cookie);
        }


        ///<summary>
        /// 添加为Cookie.Values集合
        /// (Key和Value都会进行DES加密)
        ///</summary>
        ///<param name="cookieName"></param>
        ///<param name="dic"></param>
        public static void AddCookie(string cookieName, Dictionary<string, string> dic)
        {
            var cookie = new HttpCookie(cookieName) { Expires = DateTime.Now.AddDays(10) };
            foreach (var de in dic)
            {
                cookie[de.Key] = de.Value;
            }
            AddCookie(cookie);
        }

        #endregion

        #region 读取Cookies

        /// <summary>
        /// 获得Cookie的值
        /// </summary>
        /// <param name="cookieName"></param>
        /// <returns></returns>
        public static string GetCookieValue(string cookieName, HttpContext content = null)
        {
            return GetCookieValue(cookieName, null, content);
        }

        /// <summary>
        /// 获得Cookie的子键值
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetCookieValue(string cookieName, string key, HttpContext content = null)
        {
            return GetCookieValue(GetCookie(cookieName, content), key);
        }

        /// <summary>
        /// 获得Cookie的子键值
        /// </summary>
        /// <param name="cookie"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        private static string GetCookieValue(HttpCookie cookie, string key)
        {
            if (cookie != null)
            {
                if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
                    return cookie.Values[key];
                return cookie.Value;
            }
            return string.Empty;
        }

        /// <summary>
        /// 获得Cookie
        /// </summary>
        /// <param name="cookieName"></param>
        /// <returns></returns>
        public static HttpCookie GetCookie(string cookieName, HttpContext context = null)
        {
            var request = (context ?? HttpContext.Current).Request;
            var cookies = request.Cookies[cookieName];
            return request != null ? request.Cookies[cookieName] : null;
        }

        #endregion

        #region 删除Cookie

        /// <summary>
        /// 删除Cookie
        /// </summary>
        /// <param name="cookieName"></param>
        public static void RemoveCookie(string cookieName)
        {
            RemoveCookie(cookieName, null);
        }

        /// <summary>
        /// 删除Cookie的子键
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="key"></param>
        public static void RemoveCookie(string cookieName, string key)
        {
            var response = HttpContext.Current.Response;
            if (response == null) return;
            var cookie = response.Cookies[cookieName];
            if (cookie == null) return;
            if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
                cookie.Values.Remove(key);
            else
            {
                cookie.Expires = DateTime.Now.AddDays(-1);
                response.Cookies.Add(cookie);
            }

        }

        ///<summary>
        ///</summary>
        public static void RemoveAllCookie()
        {
            var response = HttpContext.Current.Request;
            if (response == null) return;

            int cookieCount = response.Cookies.Count;
            for (int i = 0; i < cookieCount; i++)
            {
                var cookie = response.Cookies[i];
                cookie.Expires = DateTime.Now.AddDays(-1);
                response.Cookies.Add(cookie);
            }

            //foreach (HttpCookie cookie in response.Cookies)
            //{
            //    //cookie.Value = null;
            //    cookie.Expires = DateTime.Now.AddDays(-1);
            //    response.Cookies.Add(cookie);
            //}
        }

        #endregion

        #region 设置/修改Cookie

        /// <summary>
        /// 设置Cookie子键的值
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetCookie(string cookieName, string key, string value)
        {
            SetCookie(cookieName, key, value, null);
        }

        /// <summary>
        /// 设置Cookie值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetCookie(string key, string value)
        {
            SetCookie(key, null, value, null);
        }

        /// <summary>
        /// 设置Cookie值和过期时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="expires"></param>
        public static void SetCookie(string key, string value, DateTime expires)
        {
            SetCookie(key, null, value, expires);
        }

        /// <summary>
        /// 设置Cookie过期时间
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="expires"></param>
        public static void SetCookie(string cookieName, DateTime expires)
        {
            SetCookie(cookieName, null, null, expires);
        }

        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="expires"></param>
        private static void SetCookie(string cookieName, string key, string value, DateTime? expires)
        {
            var response = HttpContext.Current.Response;
            if (response == null) return;
            var cookie = response.Cookies[cookieName];
            if (cookie == null) return;
            if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
                cookie.Values.Set(key, value);
            else
                if (!string.IsNullOrEmpty(value))
                cookie.Value = value;
            if (expires != null)
                cookie.Expires = expires.Value;
            response.SetCookie(cookie);
        }

        #endregion
    }
}
